﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FonShare.Classes.Data;

namespace FonShare.Classes.FTP
{
    /// <summary>
    /// Manage FTP Connection and operations.
    /// </summary>
    /// <author>spesme</author>
    /// <since>26/06/2009</since>
    public static class FS_FTPManager
    {
        /// <summary>
        /// Gets the files from Server as List<String>.
        /// </summary>
        /// <param name="p_server">The server.</param>
        /// <returns>A list of string containing filenames.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static List<String> GetFilesList(FS_Server p_server)
        {
            List<String> result = new List<string>();
            FS_FTPFactory ftpServer = new FS_FTPFactory(p_server.IP, p_server.Path, p_server.Login, p_server.Password);
            ftpServer.login();
            result = ftpServer.GetSimpleFileList("");
            ftpServer.close();
            return result;
        }

        /// <summary>
        /// Gets the FTP files list.
        /// </summary>
        /// <param name="p_server">The server.</param>
        /// <returns>A List of FTP files.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static List<FS_FTPFile> GetFTPFilesList(FS_Server p_server)
        {
            List<FS_FTPFile> result = new List<FS_FTPFile>();
            FS_FTPFactory ftpServer = new FS_FTPFactory(p_server.IP, p_server.Path, p_server.Login, p_server.Password);
            ftpServer.login();
            result = ftpServer.GetEnhancedFileList("");
            ftpServer.close();
            return result;
        }

        /// <summary>
        /// Gets the shared files list from groups for a user.
        /// </summary>
        /// <param name="p_userId">The user id.</param>
        /// <returns>A dictionary of servers and files.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static Dictionary<string, List<FS_FTPFile>> GetSharedFilesList(int p_userId)
        {
            Dictionary<string, List<FS_FTPFile>> result = new Dictionary<string, List<FS_FTPFile>>();
            FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
            List<int> groupIdList = dc.FS_GroupUsers.Where(record => record.UserID == p_userId).Select(record => record.GroupID).ToList();
            List<int> userIdList = dc.FS_GroupUsers.Where(record => groupIdList.Contains(record.GroupID) && record.UserID != p_userId).Select(record => record.FS_User.UserID).ToList();
            userIdList.AddRange(dc.FS_Groups.Where(record => groupIdList.Contains(record.GroupID)).Select(record => record.OwnerID).ToList());
            List<FS_Server> serversList = dc.FS_Servers.Where(record => userIdList.Contains(record.OwnerID)).ToList();
            foreach(FS_Server currentServer in serversList)
            {
                result.Add(currentServer.ServerName, GetFTPFilesList(currentServer));
            }
            return result;
        }

        /// <summary>
        /// Tests the FTP server.
        /// </summary>
        /// <param name="p_server">The server.</param>
        /// <returns>True if we can connect to the ftp server.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static bool TestFTPServer(FS_Server p_server)
        {
            bool result = false;
            FS_FTPFactory ftpServer = null;
            try
            {
                ftpServer = new FS_FTPFactory(p_server.IP, p_server.Path, p_server.Login, p_server.Password);
                ftpServer.login();
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (ftpServer != null)
                {
                    try
                    {
                        ftpServer.close();
                    }
                    catch (Exception ex)
                    {
                        result = false;
                    }
                }
            }
            return result;
        }
    }
}
